/**
*	@name							Circuit
*	@descripton						This Jquery plugin makes
*	@version						1.0
*	@requires						Jquery
*
*	@author							Jan Jarfalk
*	@author-email					jan.jarfalk@unwrongest.com
*	@author-website					http://www.unwrongest.com
*
*	@licens							MIT License - http://www.opensource.org/licenses/mit-license.php
*/

(function($) {

	$.fn.circuit = function(options){
	  	
		// Default configuration properties
		var defaults = {
			'popin'	:	120,
			'speed' :	500,
			'delay' :	500
		};
		var options = $.extend(defaults, options);  
	
		this.each(function() { 
			
			var $ul = $(this);
			var $wrapper = $('<div />').css({'width':'100%','overflow':'hidden'});
			var viewport = {};
			var items = {
					'width'		:	'',
					'margin'	:	'',
					'length'	:	$('li', $ul).length
				};
			var current = 1;
			var inAnimation = false;
			
			
			// Wrap and clone elements
			$ul.wrap($wrapper);
			$('li:last', $ul).clone().prependTo($ul);
			$('li:nth-child(' + items.length + ')', $ul).clone().prependTo($ul);
			$('li:nth-child(3), li:nth-child(4)', $ul).clone().appendTo($ul);
			
			// Set node index
			$('li', $ul).each(function(index, el){
				$(this).data('index',index-1);
			});
			
			// Events
			$ul.bind('next',function(){			animate(++current);		});
			$ul.bind('previous',function(){		animate(--current);		});
			$ul.bind('pause',function(){		clearTimeout(timer);	});
			$ul.bind('play',function(){			clearTimeout(timer);	});
			
			// Key commands
			$(window).bind('keypress', function(e, index){
				if(!inAnimation && e.keyCode == 37)
					$ul.trigger('previous');
				if(!inAnimation && e.keyCode == 39)
					$ul.trigger('next');
			})
			
			// Focus item on click
			$('li', $ul).bind('click',function(el){
				animate($(this).data('index'));
				$ul.trigger('pause');
			});
			
			function render() {
				viewport = {
					'width'		:	$ul.parent().width()
				};
				
				items = {
					'width'		:	$('li:first', $ul).width(),
					'margin'	:	(viewport.width - $('li:first', $ul).width() - options.popin*2)/4,
					'length'	:	$('li', $ul).length-4
				};
				
				$('li', $ul).css({
					'margin-left'	:	items.margin,
					'margin-right'	:	items.margin
				});
				
				$ul.css({
					'width'			:	($('li', $ul).width()+items.margin*2)*$('li', $ul).length,
					'margin-left'	:	getMarginLeft(current)
				});
					
			}
			
			function getMarginLeft(item) {
				// Need to be simplyfied
				return options.popin - (items.width+items.margin) - item*(2*items.margin+items.width)
			}
			
			function animate(goal, automatic) {
				
				inAnimation = true;
				
				
				clearTimeout(timer);

				
				current = goal;
				
				var callback = function(){								

			  		if(current > items.length){
						current = 1;
						$ul.css({'margin-left':getMarginLeft(current)});
					} else if (current <= 0) {
						current = (items.length);
						$ul.css({'margin-left':getMarginLeft(current)});
					}
					
					inAnimation = false;
					
					if(automatic) {
						timer = setTimeout(function(){animate(++current,true);},options.delay);
					}

				}
					
			
				$ul.animate({
						'margin-left'	:	getMarginLeft(goal)
					},
					{
						queue		:	false,
						duration	:	options.speed,
						complete	:	callback
					}
				);
			}
			
			render();
			
			timer = setTimeout(function(){
				animate(++current,true);
			}, options.delay);
			
			$(window).bind('resize', render);
						
		});
	  
	};

})(jQuery);